package com.xx.controller.xunyou.manager;

import com.xx.admin.entity.AdminUser;
import com.xx.controller.util.BaseController;
import com.xx.springBootUtil.result.JsonResult;
import com.xx.springBootUtil.util.ObjectHelper;
import com.xx.springBootUtil.validate.ValidateWriteIn;
import com.xx.springBootUtil.validate.ValidateWriteInPost;
import com.xx.util.page.PageInfo;
import com.xx.util.result.JsonUtil;
import com.xx.xunyou.entity.item.XunyouItemType;
import com.xx.xunyou.entity.item.chizhuzaixiang.Chizhuzaixiang;
import com.xx.xunyou.entity.item.chizhuzaixiang.ChizhuzaixiangContent;
import com.xx.xunyou.service.ChizhuzaixiangService;
import com.xx.xunyou.service.ItemTypeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zhl
 * @Description:
 */
@RestController
@ApiIgnore
public class ChizhuzaixiangController extends BaseController {

    @Autowired
    private ChizhuzaixiangService chizhuzaixiangService;

    @Resource
    PageInfo pageInfo;

    @Autowired
    private ItemTypeService itemTypeService;

    /**
     * 查询
     * @param title
     * @param typeId
     * @param modelAndView
     * @return
     * @throws NumberFormatException
     * @throws Exception
     */
    @RequestMapping("manager/chizhuzaixiang/query")
    public ModelAndView queryList(String title, @RequestParam(defaultValue = "0") Integer typeId, ModelAndView modelAndView) throws Exception{

        Map<String, Object> map = new HashMap<>();
        map.put("title", title);
        map.put("typeId", typeId);
        Integer totalRow = chizhuzaixiangService.queryListCountToManager(map);
        pageInfo.format(totalRow, request); //默认15页
        map.put("index", pageInfo.getPageSize()*(pageInfo.getPageNumber()-1));
        map.put("last", pageInfo.getPageSize());
        List<Map> list = chizhuzaixiangService.queryListToManager(map);
        //查询所有分类
        List<XunyouItemType> itemTypes = itemTypeService.querySonByParentKeyToManager("chizhuzaixiangcun");
        map.put("itemTypes", itemTypes);
        modelAndView.addObject("map", map);
        modelAndView.addObject("list", list);
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.setViewName("manager/chizhuzaixiang/chizhuzaixiangList");
        return modelAndView;
    }

    /**
     * 添加信息
     * @return
     * @throws Exception
     */
    @ValidateWriteInPost(parameter={"title","cover","content"},msg={"标题不能为空！","封面必传！","内容不能为空！"})
    @RequestMapping("manager/chizhuzaixiang/insert")
    public ModelAndView insert(Chizhuzaixiang chizhuzaixiang, String content) throws Exception{

        if(ObjectHelper.getOrPost(request)){
            //查询所有分类
            List<XunyouItemType> itemTypes = itemTypeService.querySonByParentKeyToManager("chizhuzaixiangcun");
            return new ModelAndView("manager/chizhuzaixiang/chizhuzaixiangInsert")
                    .addObject("itemTypes", itemTypes);
        }else{
            AdminUser adminUser = sessionAdminUser();
            chizhuzaixiang.setUserId(adminUser.getUserId());
            chizhuzaixiangService.insert(chizhuzaixiang,content);

            responseJson(JsonUtil.jsonRefresh("添加成功"));
        }
        return null;
    }

    /**
     * 修改信息
     * @return
     * @throws Exception
     */
    @ValidateWriteIn(parameter={"chizhuzaixiangId"},msg={"id不能为空！"})
    @ValidateWriteInPost(parameter={"chizhuzaixiangId","title","cover","content"},msg={"id不能为空！","标题不能为空！","封面必传！","内容不能为空！"})
    @RequestMapping("manager/chizhuzaixiang/update")
    public ModelAndView update(Chizhuzaixiang chizhuzaixiang,String content) throws Exception{

        if(ObjectHelper.getOrPost(request)){
            Chizhuzaixiang update =  chizhuzaixiangService.queryByIdToManager(chizhuzaixiang.getChizhuzaixiangId());
            ChizhuzaixiangContent updateContent = chizhuzaixiangService.queryContentByIdToManager(chizhuzaixiang.getChizhuzaixiangId());
            return new ModelAndView("manager/chizhuzaixiang/chizhuzaixiangUpdate")
                    .addObject("chizhuzaixiang", update).addObject("content", updateContent);
        }else{
            chizhuzaixiangService.update(chizhuzaixiang,content);

            responseJson(JsonUtil.jsonRefresh("修改成功"));
        }
        return null;
    }
    /**
     * 删除信息
     * @return
     * @throws Exception
     */
    @ValidateWriteIn(parameter={"chizhuzaixiangId"},msg={"id不能为空！"})
    @RequestMapping("manager/chizhuzaixiang/delete")
    public JsonResult delete(Integer chizhuzaixiangId) throws Exception{
        chizhuzaixiangService.delete(chizhuzaixiangId);
        return JsonUtil.jsonRefresh("删除成功");

    }

    /**
     * 推荐
     * @return
     * @throws Exception
     */
    @ValidateWriteIn(parameter={"chizhuzaixiangId"},msg={"id不能为空！"})
    @RequestMapping("manager/chizhuzaixiang/recommend")
    public JsonResult recommend(Integer chizhuzaixiangId) throws Exception{
        chizhuzaixiangService.recommend(chizhuzaixiangId);
        return JsonUtil.jsonRefresh("推荐成功");

    }

    /**
     * 取消推荐
     * @return
     * @throws Exception
     */
    @ValidateWriteIn(parameter={"chizhuzaixiangId"},msg={"id不能为空！"})
    @RequestMapping("manager/chizhuzaixiang/notRecommend")
    public JsonResult notRecommend(Integer chizhuzaixiangId) throws Exception{
        chizhuzaixiangService.notRecommend(chizhuzaixiangId);
        return JsonUtil.jsonRefresh("取消成功");

    }
}
